using System;
using System.Collections.ObjectModel;
using System.Threading.Tasks;
using System.Windows;
using WPFServices.Commands;
using WPFServices.Models.Domain;
using WPFServices.Services.DomainService.BomInfo;

namespace WPFServices.ViewModels.DomainViewModel.BomInfo
{
    public class BomSelectionViewModel : BaseViewModel
    {
        private readonly BomService _bomService;
        private ObservableCollection<BomDto> _boms;
        private BomDto _selectedBom;
        private string _searchBomCode;
        private string _searchProductName;
        private bool _isLoading;

        public BomSelectionViewModel()
        {
            _bomService = new BomService();
            _boms = new ObservableCollection<BomDto>();

            SearchCommand = new RelayCommand(async _ => await SearchBomsAsync());
            ResetCommand = new RelayCommand(async _ => await LoadBomsAsync());
            ConfirmCommand = new RelayCommand(_ => ConfirmSelection());
            CancelCommand = new RelayCommand(_ => CancelSelection());

            // 初始加载数据
            _ = LoadBomsAsync();
        }

        public event Action<bool> CloseWindow;

        #region 属性
        public ObservableCollection<BomDto> Boms
        {
            get => _boms;
            set => SetProperty(ref _boms, value);
        }

        public BomDto SelectedBom
        {
            get => _selectedBom;
            set => SetProperty(ref _selectedBom, value);
        }

        public string SearchBomCode
        {
            get => _searchBomCode;
            set => SetProperty(ref _searchBomCode, value);
        }

        public string SearchProductName
        {
            get => _searchProductName;
            set => SetProperty(ref _searchProductName, value);
        }

        public bool IsLoading
        {
            get => _isLoading;
            set => SetProperty(ref _isLoading, value);
        }
        #endregion

        #region 命令
        public RelayCommand SearchCommand { get; }
        public RelayCommand ResetCommand { get; }
        public RelayCommand ConfirmCommand { get; }
        public RelayCommand CancelCommand { get; }
        #endregion

        #region 方法
        private async Task LoadBomsAsync()
        {
            try
            {
                IsLoading = true;
                SearchBomCode = string.Empty;
                SearchProductName = string.Empty;

                var searchDto = new SearchBomDto
                {
                    BomCode = string.Empty,
                    ProductName = string.Empty,
                    BomVersion = string.Empty,
                    PageIndex = 1,
                    PageSize = 100
                };

                var result = await _bomService.SearchBomAsync(searchDto);

                Boms.Clear();
                if (result.Data != null)
                {
                    foreach (var bom in result.Data)
                    {
                        Boms.Add(bom);
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show($"加载BOM列表失败: {ex.Message}", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
            }
            finally
            {
                IsLoading = false;
            }
        }

        private async Task SearchBomsAsync()
        {
            try
            {
                IsLoading = true;

                var searchDto = new SearchBomDto
                {
                    BomCode = SearchBomCode?.Trim(),
                    ProductName = SearchProductName?.Trim(),
                    BomVersion = string.Empty,
                    PageIndex = 1,
                    PageSize = 100
                };

                var result = await _bomService.SearchBomAsync(searchDto);

                Boms.Clear();
                if (result.Data != null)
                {
                    foreach (var bom in result.Data)
                    {
                        Boms.Add(bom);
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show($"搜索BOM失败: {ex.Message}", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
            }
            finally
            {
                IsLoading = false;
            }
        }

        private void ConfirmSelection()
        {
            if (SelectedBom == null)
            {
                MessageBox.Show("请选择一个BOM", "提示", MessageBoxButton.OK, MessageBoxImage.Warning);
                return;
            }

            CloseWindow?.Invoke(true);
        }

        private void CancelSelection()
        {
            SelectedBom = null;
            CloseWindow?.Invoke(false);
        }
        #endregion
    }
} 